import QtQuick 2.0
import QtQuick.Controls 2.0
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import Log 1.0
import Translation 1.0
import "qrc:/CustomComponent"

CusWindowMask {
    id: cusRenameDialogRoot
    property string title: ""
    property string oldName: ""
    property bool bRenaming: false

    signal doRename(newName:string)

    onVisibleChanged: {
        if( !visible ){
            oldName = ""
        }
        bRenaming = false
    }

    onOldNameChanged: {
        printerNameTextField.text = oldName
    }

    function errorOccurs(errorMsg){
        printerNameTextField.errorOccurs = true
        errorText.text = errorMsg
    }

    function errorClear(){
        printerNameTextField.errorOccurs = false
    }

    function newNameCheck(newName){
        if( newName === oldName ){
            errorOccurs(TransText.other_enter_name)
            return false
        }else if(newName === ""){
            errorOccurs(TransText.cloud_TNCBE)
            return false
        }
        return true
    }

    CusWindow_w {
        id: win
        width: 460
        height: 200
        title: cusRenameDialogRoot.title
        anchors.centerIn: parent
        onClose: {
            cusRenameDialogRoot.close()
        }

        Rectangle {
            id: contentBgRect
            color: "transparent"
            anchors.fill: parent
            anchors.topMargin: win.titleHeight
            radius: win.windowRadius

            Rectangle {
                id: contentRect
                anchors.top: parent.top
                anchors.bottom: statusBarRect.top
                anchors.left: parent.left
                anchors.right: parent.right

                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: 20
                    spacing: 10

                    CusText {
                        text: TransText.cloud_PPANN
                        color: Theme.text_color

                        font.pixelSize: Theme.font_size_13
                    }

                    ColumnLayout {
                        Layout.fillWidth: true
                        spacing: 0

                        CusTextField {
                            id: printerNameTextField
                            Layout.fillWidth: true
                            implicitHeight: 28
                            onTextChanged: { errorOccurs = false }
                        }

                        CusText {
                            id: errorText
                            color: Theme.error_color
                            font.pixelSize: Theme.font_size_13
                            visible: printerNameTextField.errorOccurs
                        }
                    }

                    Item {
                        Layout.fillHeight: true
                    }
                }
            }

            Rectangle {
                id: statusBarRect
                color: "transparent"
                implicitHeight: 30
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.rightMargin: 20
                anchors.bottomMargin: win.windowRadius

                CusSolidBtn {
                    id: oKBtn
                    anchors.right: parent.right
                    anchors.verticalCenter: parent.verticalCenter
                    implicitWidth: 96
                    implicitHeight: 30
                    _text: bRenaming ? "" : TransText.app_OK

                    AnimatedImage  {
                        anchors.centerIn: parent
                        source: "qrc:/assets/myfilelist/add_printer_loading_32x32.gif"
                        visible: bRenaming
                    }

                    onClicked: {
                        if(!newNameCheck(printerNameTextField.text)) return
                        bRenaming = true
                        doRename(printerNameTextField.text)
                    }
                }

                CusHollowBtn {
                    id: cancelBtn
                    anchors.right: oKBtn.left
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.rightMargin: 20
                    implicitWidth: 96
                    implicitHeight: 30
                    _text: TransText.app_Cancel
                    onClicked: { cusRenameDialogRoot.close() }
                }
            }
        }
    }
}


